﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Report
{
    public partial class frmjcczmx : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmjcczmx()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == string.Empty && txtcardID.Text == string.Empty)
            {
                selecttime();
            }
            else if (comboBox1.Text != string.Empty && txtcardID.Text == string.Empty)
            {
                selctxm();
            }
            else if (comboBox1.Text == string.Empty && txtcardID.Text != string.Empty)
            {
                selectcard();
            }
            else

            {
                selectall();
            }
        }
        public DataSet selecttime()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format("SELECT [ID],[cardid],[持卡人],[项目名称] ,[充值次数],[项目单价] ,[项目金额] ,[经手人],[充值时间] ,[单号],case if1 when 0 then '消' when 1 then '充' end as 标识 FROM [BHairDB].[dbo].[项目充值明细]where 充值时间>='{0}' and 充值时间<='{1}'", dtpbegin.Text,dtpend.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds, "计次充值");
            dataGridView1.DataSource= ds.Tables["计次充值"];
            conn.Close();



            return ds;
        }
        public DataSet selctxm()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT [ID],[cardid],[持卡人],[项目名称] ,[充值次数],[项目单价] ,[项目金额] ,[经手人],[充值时间] ,[单号],case if1 when 0 then '消' when 1 then '充' end as 标识 FROM [项目充值明细]where 充值时间>='{0}' and 充值时间<='{1}' and 项目名称='{2}'", dtpbegin.Text, dtpend.Text,comboBox1.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.Fill(ds, "计次充值");
            dataGridView1.DataSource = ds.Tables["计次充值"];
            conn.Close();
            return ds;
        }
        public DataSet selectcard()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT [ID],[cardid],[持卡人],[项目名称] ,[充值次数],[项目单价] ,[项目金额] ,[经手人],[充值时间] ,[单号] ,case if1 when 0 then '消' when 1 then '充' end as 标识 FROM [项目充值明细] where 充值时间>='{0}' and 充值时间<='{1}' and cardid='{2}'", dtpbegin.Text, dtpend.Text,txtcardID.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.Fill(ds, "计次充值");
            dataGridView1.DataSource = ds.Tables["计次充值"];
            conn.Close();
            return ds;
        }
        public DataSet selectall()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT [ID],[cardid],[持卡人],[项目名称] ,[充值次数],[项目单价] ,[项目金额] ,[经手人],[充值时间] ,[单号] ,case if1 when 0 then '消' when 1 then '充' end as 标识 FROM [项目充值明细]where 充值时间>='{0}' and 充值时间<='{1}' and cardid='{2}' and 项目名称 ='{3}' ", dtpbegin.Text, dtpend.Text, txtcardID.Text,comboBox1.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.Fill(ds, "计次充值");
            dataGridView1.DataSource = ds.Tables["计次充值"];
            conn.Close();
            return ds;
        }

        private void frmjcczmx_Load(object sender, EventArgs e)
        {
            jiazaixm();
        }
        /// <summary>
        /// 加载计次项目
        /// </summary>
        public void jiazaixm()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT [id],[iid] ,[iname] FROM [BHairDB].[dbo].[items] where jc=1";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds, "items");
            comboBox1.DataSource= ds.Tables["items"];
            comboBox1.DisplayMember = "iname";
            comboBox1.SelectedIndex = -1;
            conn.Close();

        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if (e.RowIndex < dataGridView1.Rows.Count - 1)
            {
                DataGridViewRow dgrSingle = dataGridView1.Rows[e.RowIndex];
                try
                {
                    if (dgrSingle.Cells["标识"].Value.ToString().Contains("消"))
                    {
                        dgrSingle.DefaultCellStyle.ForeColor = Color.Red;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
       e.RowBounds.Location.Y,
       dataGridView1.RowHeadersWidth - 4,
       e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}
